///////////////////
//BLOCK FONCTIONS TECHNIQUES
///////////////////
//function checkAuth(page) 
//function inputMoisEnCours(p_champs1, p_champs2) 
//function inputMoisEnCoursRH(p_champs1, p_champs2) 

///////////////////
//BLOCK FONCTIONS METIER
///////////////////
//function init_commun()

///////////////////
//BLOCK FONCTIONS AJAX JS/PHP/MYSQL
///////////////////
//function getTabRapportRHuser(p_user)
//function getTabUsers(date) 
//function getTabUsers_lv1() 

///////////////////
//BLOCK FONCTIONS AFFICHAGE
///////////////////
//function show_commun() 
//function getHtmlSuiviKO(p_user)
//function intToTimeChar(intTime) 

//---------------------------------------------------------

///////////////////
//BLOCK VARIABLE GLOBAL
///////////////////

///////////////////
//BLOCK FONCTIONS TECHNIQUES
///////////////////
function inputMoisEnCours(p_champs1, p_champs2) {
	try {
	
		var element1 = document.getElementById(p_champs1);
		var element2 = document.getElementById(p_champs2);
		
		var d = new Date();
		var dm = d.getMonth() + 1;
		var dan = d.getYear();
		if(dan < 999) dan+=1900;
		var nbjour = daysInMonth(dm,dan);
		
		element1.value = "01/"+pad2(dm)+"/"+dan;
		element2.value = nbjour+"/"+pad2(dm)+"/"+dan;
		
	}
	catch (er) {
		log(0, "ERROR(inputMoisEnCours):" + er.message);
	}
}

function inputMoisEnCoursRH(p_champs1, p_champs2) {
	try {
	
		var element1 = document.getElementById(p_champs1);
		var element2 = document.getElementById(p_champs2);
		
		var d = new Date();
		var dm = d.getMonth() + 1;
		var dan = d.getYear();
		var dd = d.getDate();
		if(dan < 999) dan+=1900;
		if(dd == 1) {dd = 1;} else {dd -= 1;}
		
		element1.value = "01/"+pad2(dm)+"/"+dan;
		element2.value = pad2(dd)+"/"+pad2(dm)+"/"+dan;
		
	}
	catch (er) {
		log(0, "ERROR(inputMoisEnCours):" + er.message);
	}
}

function inputSemaineEnCours(p_champs1, p_champs2) {
	try {
	
		var element1 = document.getElementById(p_champs1);
		var element2 = document.getElementById(p_champs2);
		
		var d = new Date();
		var dm = d.getMonth() + 1;
		var dan = d.getYear();
		var dd = d.getDate();
		if(dan < 999) dan+=1900;
		
		element2.value = pad2(dd)+"/"+pad2(dm)+"/"+dan;

		d.setDate(d.getDate() - 1);
		while(d.getDay() != 1) 
		{ 
		   d.setDate(d.getDate() - 1);
		} 
		
		var dm = d.getMonth() + 1;
		var dan = d.getYear();
		var dd = d.getDate();
		if(dan < 999) dan+=1900;
		
		element1.value = pad2(dd)+"/"+pad2(dm)+"/"+dan;
		
	}
	catch (er) {
		log(0, "ERROR(inputSemaineEnCours):" + er.message);
	}
}

function inputDeuxSemaines(p_champs1, p_champs2) {
	try {
	
		var element1 = document.getElementById(p_champs1);
		var element2 = document.getElementById(p_champs2);
		
		var d = new Date();
		var dm = d.getMonth() + 1;
		var dan = d.getYear();
		var dd = d.getDate();
		if(dan < 999) dan+=1900;
		
		element2.value = pad2(dd)+"/"+pad2(dm)+"/"+dan;

		d.setDate(d.getDate() - 7);
		while(d.getDay() != 1) 
		{ 
		   d.setDate(d.getDate() - 1);
		} 
		
		var dm = d.getMonth() + 1;
		var dan = d.getYear();
		var dd = d.getDate();
		if(dan < 999) dan+=1900;
		
		element1.value = pad2(dd)+"/"+pad2(dm)+"/"+dan;
		
	}
	catch (er) {
		log(0, "ERROR(inputDeuxSemaines):" + er.message);
	}
}

///////////////////
//BLOCK FONCTIONS METIER
///////////////////


///////////////////
//BLOCK FONCTIONS AJAX JS/PHP/MYSQL
///////////////////
function getTabRapportRHuser(p_user) {
	try {
		var strResponse = "";
		var returns = new Array();
		var tempTab = new Array();
		var tempSubTab = new Array();

		xhr_object = new AJ();

		var url = "phpsql/mysql_getDataRapportRH2User.php?milis=" + getMilise() + "&user=" + p_user;

		xhr_object.open("GET", url, false);
		xhr_object.send(null);
		if (xhr_object.readyState == 4) {
			strResponse = xhr_object.responseText;
		} else {
			strResponse = "ERROR";
		}

		tempTab = strResponse.split("\n");

		for (var indice in tempTab) {
			tempSubTab = tempTab[indice].split("&");
			if (tempSubTab.length > 1) {
				returns[returns.length] = tempSubTab.slice(0, tempSubTab.length - 1);
			}
		}

		delete xhr_object;

		return returns;
	}
	catch (er) {
		log(0, "ERROR(getTabRapportRHuser):" + er.message);
	}
}

function getTabUsers(date) {
    try {
        var strResponse = "";
        var returns = new Array();
        var tempTab = new Array();
        var tempSubTab = new Array();

        xhr_object = new AJ();

        var url = "phpsql/mysql_getUsers.php?milis="+getMilise()+"&date="+date+"&crtl=gard";

        xhr_object.open("GET", url, false);
        xhr_object.send(null);
        if (xhr_object.readyState == 4) {
            strResponse = xhr_object.responseText;
        } else {
            strResponse = "ERROR";
        }

        tempTab = strResponse.split("\n");

        for (var indice in tempTab) {
            tempSubTab = tempTab[indice].split("&");
            if (tempSubTab.length > 1) {
                if (tempSubTab[1] == "") {
                    tempSubTab[1] = tempSubTab[0];
                }
                returns[tempSubTab[0]] = tempSubTab.slice(0, tempSubTab.length - 1);
            }
        }

        delete xhr_object;

        return returns;
    }
    catch (er) {
        log(0, "ERROR(getTabUsers):" + er.message);
    }
}

function getTabUsers_lv1() {
    try {
        var strResponse = "";
        var returns = new Array();
        var tempTab = new Array();
        var tempSubTab = new Array();

        xhr_object = new AJ();

        var url = "phpsql/mysql_getUsers_lv1.php?milis="+getMilise()+"&crtl=gard";

        xhr_object.open("GET", url, false);
        xhr_object.send(null);
        if (xhr_object.readyState == 4) {
            strResponse = xhr_object.responseText;
        } else {
            strResponse = "ERROR";
        }

        tempTab = strResponse.split("\n");

        for (var indice in tempTab) {
            tempSubTab = tempTab[indice].split("&");
            if (tempSubTab.length > 1) {
                if (tempSubTab[1] == "") {
                    tempSubTab[1] = tempSubTab[0];
                }
                returns[tempSubTab[0]] = tempSubTab.slice(0, tempSubTab.length - 1);
            }
        }

        delete xhr_object;

        return returns;
    }
    catch (er) {
        log(0, "ERROR(getTabUsers_lv1):" + er.message);
    }
}

///////////////////
//BLOCK FONCTIONS AFFICHAGE
///////////////////

function getHtmlSuiviKO(p_user) {
	try {
		var tab = new Array();
		tab = getTabRapportRHuser(p_user);
		//[X][0] = date
		//[X][1] = responsable
		//[X][2] = total
		
		var strRetour = "";
		
		if(tab.length > 0){
					
			strRetour += "<center>";
			strRetour += "<br>";
			strRetour += "<img border=0 src=\"img/mini_attention.png\" /> Vous n'avez pas rempli enti&egrave;rement votre suivi hebdomadaire.";
			strRetour += "<br>";
			
			strRetour += "			<table cellpadding=\"0\" cellspacing=\"0\" border=1 style=\"font-size:14px;background-color:white;\">";
			
			strRespon = "";
			
			strRetour += "				<tr>";
			strRetour += "					<td style=\"width: 100px;text-align:center;\">";
			strRetour += "						Responsable";
			strRetour += "					</td>";
			strRetour += "					<td style=\"width: 150px;text-align:center;\">";
			strRetour += "						Non compl&eacute;t&eacute;";
			strRetour += "					</td>";
			
			for (var indice in tab) {
				if(strRespon != tab[indice][1]){
					strRetour += "					</td>";
					strRetour += "				</tr>";
					strRespon = tab[indice][1];
					strRetour += "				<tr>";
					strRetour += "					<td style=\"width: 100px;text-align:center;\">";
					strRetour += "						"+tab[indice][1]+"";
					strRetour += "					</td>";
					strRetour += "					<td style=\"width: 150px;text-align:center;font-size:10px;\">";
				}
				strRetour += "				"+tab[indice][0]+"("+tab[indice][2]+")<br>";
			}
			strRetour += "					</td>";
			strRetour += "				</tr>";
			strRetour += "			</table>";
			strRetour += "</center>";
			
		}
		

        return strRetour;
	}
	catch (er) {
		log(0, "ERROR(getHtmlSuiviKO):" + er.message);
	}
}

function intToTimeChar(intTime) {
    try {
        if ((!isNaN(intTime)) & (intTime != 0)) {
            nbMinute = Math.round(intTime) % 60;
            nbHeure = (Math.round(intTime) - nbMinute) / 60;
            nbHeure = nbHeure % 10;
            nbJour = (Math.round(intTime) - (nbHeure * 60) - nbMinute) / 600;
            strTime = nbJour + "j" + nbHeure + "h" + nbMinute;
        } else {
            strTime = "0";
        }
        return strTime;
    }
    catch (er) {
        log(0, "ERROR(intToTimeChar):" + er.message);
        return "xjxhx";
    }
}